package com.jiuyin.dianjing.ui.activity.setting;

import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.BaseRecyclerAdapter;
import com.jiuyin.dianjing.adapter.SmartViewHolder;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.interfaces.ProgressDialogCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.decoration.CustomDividerItemDecoration;
import com.jiuyin.dianjing.model.UserItemModel;
import com.jiuyin.dianjing.ui.activity.base.BaseRefreshActivity;
import com.jiuyin.dianjing.util.DisplayUtil;
import com.jiuyin.dianjing.util.LogUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_LIST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_PAGE_NUM;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.PAGE_NUM;

/**
 * 粉丝
 */
public class SettingMyFansActivity extends BaseRefreshActivity<UserItemModel> {

    public SettingMyFansActivity() {
        super(PAGE_NUM);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_setting_my_fans;
    }

    @Override
    protected void initData() {
        mAdapter = new BaseRecyclerAdapter<UserItemModel>(R.layout.item_user) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, UserItemModel model, int position) {
                if (null != model) {
                    holder.text(R.id.tv_name, model.getNickname());
                    ImageView ivGame = holder.itemView.findViewById(R.id.img_header);
                    Glide.with(SettingMyFansActivity.this)
                            .load(model.getHeader())
                            .override(DisplayUtil.dp2px(SettingMyFansActivity.this, 27), DisplayUtil.dp2px(SettingMyFansActivity.this, 27))
                            .into(ivGame);
                }
            }
        };
        mRcvPage.addItemDecoration(new CustomDividerItemDecoration(this));//添加分割线
        mRcvPage.setAdapter(mAdapter);
        fetchData();
    }

    @Override
    protected void initView() {
        setTitle(R.string.my_fans);
    }

    @Override
    protected void fetchData() {
        HashMap<String, Object> data = new HashMap<>();
        data.put(KEY_TOKEN, HelperApplication.getToken());
        data.put(KEY_PAGE_NUM, mPageNum);
        JSONObject jsonObject = new JSONObject(data);
        ProgressDialogCallback progress = new ProgressDialogCallback() {
            @Override
            public void showProgressDialog() {
                showLoading();
            }

            @Override
            public void dismissProgressDialog() {
                dismissLoading();
            }
        };
        ApiResultCallback request = new ApiResultCallback() {
            @Override
            public void onSuccess(JsonObject data) {
                if (null != data && data.has(KEY_LIST)) {
                    List<UserItemModel> userItemModels = new Gson().fromJson(data.getAsJsonArray(KEY_LIST), new TypeToken<List<UserItemModel>>() {
                    }.getType());
                    dealWithSuccessInfo(userItemModels);
                } else {
                    dealOtherInfo();
                }
            }

            @Override
            public void onFail(String msg) {
                LogUtil.log("SettingMyFansActivity onFail msg = " + msg);
                dealOtherInfo();
            }

            @Override
            public void onError(String msg) {
                LogUtil.log("SettingMyFansActivity onFail msg = " + msg);
                dealOtherInfo();
            }
        };
        ServerApi.post(ApiEnum.APP_GET_USER_FANS_LIST_API.getUrl(), jsonObject, null,
                progress, d -> addDisposable(d), request);
    }
}
